package com.ruoyi.quality.controller;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.DateUtils;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.quality.domain.QualityCustom;
import com.ruoyi.quality.service.IQualityCustomService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 客户管理Controller
 *
 * @author wky
 * @date 2024-06-05
 */
@RestController
@RequestMapping("/quality/custom")
@Tag(name = "【客户管理】管理")
public class QualityCustomController extends BaseController
{
    @Autowired
    private IQualityCustomService qualityCustomService;

    /**
     * 查询客户管理列表
     */
    @Operation(summary = "查询客户管理列表")
    @PreAuthorize("@ss.hasPermi('quality:custom:list')")
    @GetMapping("/list")
    public TableDataInfo list(QualityCustom qualityCustom)
    {
        startPage();
        List<QualityCustom> list = qualityCustomService.selectQualityCustomList(qualityCustom);
        return getDataTable(list);
    }

    @Operation(summary = "查询客户管理列表")
    @PreAuthorize("@ss.hasPermi('quality:custom:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll()
    {
        List<QualityCustom> list = qualityCustomService.selectQualityCustomList(null);
        return AjaxResult.success(list);
    }

    /**
     * 导出客户管理列表
     */
    @Operation(summary = "导出客户管理列表")
    @PreAuthorize("@ss.hasPermi('quality:custom:export')")
    @Log(title = "客户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QualityCustom qualityCustom)
    {
        List<QualityCustom> list = qualityCustomService.selectQualityCustomList(qualityCustom);
        ExcelUtil<QualityCustom> util = new ExcelUtil<QualityCustom>(QualityCustom.class);
        util.exportExcel(response, list, "客户管理数据");
    }

    /**
     * 获取客户管理详细信息
     */
    @Operation(summary = "获取客户管理详细信息")
    @PreAuthorize("@ss.hasPermi('quality:custom:query')")
    @GetMapping(value = "/{customId}")
    public AjaxResult getInfo(@PathVariable("customId") Long customId)
    {
        return success(qualityCustomService.selectQualityCustomByCustomId(customId));
    }

    /**
     * 新增客户管理
     */
    @Operation(summary = "新增客户管理")
    @PreAuthorize("@ss.hasPermi('quality:custom:add')")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QualityCustom qualityCustom)
    {
        return toAjax(qualityCustomService.insertQualityCustom(qualityCustom));
    }

    /**
     * 修改客户管理
     */
    @Operation(summary = "修改客户管理")
    @PreAuthorize("@ss.hasPermi('quality:custom:edit')")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QualityCustom qualityCustom)
    {
        return toAjax(qualityCustomService.updateQualityCustom(qualityCustom));
    }

    /**
     * 删除客户管理
     */
    @Operation(summary = "删除客户管理")
    @PreAuthorize("@ss.hasPermi('quality:custom:remove')")
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{customIds}")
    public AjaxResult remove(@PathVariable( name = "customIds" ) Long[] customIds)
    {
        return toAjax(qualityCustomService.deleteQualityCustomByCustomIds(customIds));
    }

    @GetMapping("/getCurrenYearData")
    @Anonymous
    public AjaxResult getCurrentMonthCustomCount(){
        int year = LocalDateTime.now().getYear();
        List<Map<Long, Long>> monthCountMap = qualityCustomService.getMonthCountMap((long) year);
        return success(monthCountMap);
    }

    @Anonymous
    @GetMapping("/lastFiveCustomCount")
    public AjaxResult getLast5YearCustomCount(){
        int year = LocalDateTime.now().getYear();
        List<Map<Long, Long>> monthCountBetween = qualityCustomService.getMonthCountBetween((long) (year - 5), (long) year);
        return success(monthCountBetween);
    }
}
